function TreeNode(val, left, right) {
    this.val = (val === undefined ? 0 : val);
    this.left = (left === undefined ? null : left);
    this.right = (right === undefined ? null : right);
}

// 前序遍历
var preorderTraversal = function(root) {
    let res = [];
    const dfs = function (root) {
        if (root === null)
            return;
        res.push(root.val);
        dfs(root.left);
        dfs(root.right);
    }

    dfs(root);
    return res;
}

// 层序遍历
var levelOrder = function (root) {
    // 二叉树的层序遍历
    let res = [], queue = [];
    queue.push(root);
    if (root === null)
        return res;
    while (queue.length !== 0) {
        // 当前层级结点数
        let len = queue.length;
        let curLevel = [];
        for (let i = 0; i < len; i++) {
            let node = queue.shift();
            curLevel.push(node.val);
            if (node.left)
                queue.push(node.left);
            if (node.right)
                queue.push(node.right);
        }
        res.push(curLevel);
    }
    return res;
}

